import Mock from 'mockjs'

let Random = Mock.Random;
const List = []
const count = 500

for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: '@increment',
    username: '@cname',
    password: '123456',
    "roles|1": ['admin','editor'],
    avatar: Random.dataImage('30x30'), //头像
    timestamp: + Random.date('T'), //时间戳
    status: 1   //1：有效 0：删除
  }))
}

export default {
  getList: config => {
    const { username, page = 1, limit = 20 } = config.params

    let mockList = List.filter(item => {
      if(item.status === 0) return false;
      if (username &&  item.username.indexOf(username) < 0) return false
      return true
    })

    const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

    return [200,{
      total: mockList.length,
      items: pageList,
    }]
  },
  uploadAvatar: (config) => {
    console.log(11);
    return [200,{data: '上传成功'}]
  },
  createUser: config => {
    console.log(config,111);
    const { username,password,avatar,roles,timestamp } = JSON.parse(config.data)
    var id = parseInt(Math.random()*100)+1024;
    List.push({
      id,
      username,
      password,
      avatar,
      roles,
      timestamp
    })
    return [200,{
      code: 200,
      msg: '添加成功'
    }]
  },
  updateUser: config => {
    const { id } = JSON.parse(config.data)
    List.find(function(currentValue, index, arr){
          if(id === currentValue.id){
            List[index] = JSON.parse(config.data)
          }
    })
    return [200,{
      code: 200,
      msg: '修改成功'
    }]
  },
  deleteUser: config => {
    const { id } = JSON.parse(config.data)
    List.find(function(currentValue, index, arr){
      if(id === currentValue.id){
        List[index].status = 0
      }
    })
    return [200,{
      code: 200,
      msg: '删除成功'
    }]
  }
}
